package com.liao.product.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.liao.common.constant.SessionEnum;
import com.liao.common.constant.StatusCodeEnum;
import com.liao.common.doc.ProductDoc;
import com.liao.common.entity.member.UserEntity;
import com.liao.common.entity.product.ProductEntity;
import com.liao.common.utils.PageUtils;
import com.liao.common.utils.Result;
import com.liao.common.vo.SearchResult;
import com.liao.common.vo.product.ProductDetailVO;
import com.liao.common.vo.product.ProductSaveReq;
import com.liao.product.feign.SearchFeignService;
import com.liao.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;


/**
 * 商品SPU信息表
 *
 * @author liao
 * @email 2858015963@qq.com
 * @date 2025-02-13 13:26:53
 */
@RestController
@RequestMapping("product/product")
public class ProductController {


    @Autowired
    private ProductService productService;

    @Autowired
    private SearchFeignService searchFeignService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public PageUtils<ProductEntity> list(@RequestParam Map<String, Object> params) {
        return productService.queryPage(params);

    }


    /**
     * 信息
     */
    @RequestMapping("/info/{productId}")
    public ProductEntity info(@PathVariable("productId") Integer productId) {
        return productService.getById(productId);
    }

//    /**
//     * 保存
//     */
//    @RequestMapping("/save")
//    public R save(@RequestBody ProductEntity product){
//		productService.save(product);
//
//        return R.ok();
//    }


    /**
     * 保存
     */
    @PostMapping("/save")
    public void save(@RequestBody ProductSaveReq saveReq) {
        productService.save(saveReq);

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public void update(@RequestBody ProductEntity product) {
        productService.updateById(product);

    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public void delete(@RequestBody Integer[] productIds) {
        productService.removeByIds(Arrays.asList(productIds));

    }

    /**
     * 猜你喜欢
     *
     * @return
     * @Param from 从第几个开始 页码默认值1
     * @Param from 页大小默认是10
     */
    @GetMapping("/guessLike")
    public SearchResult<ProductDoc> guessLike(@RequestParam(defaultValue = "0") Integer from, @RequestParam(defaultValue = "10") Integer size) {
        UserEntity user = new UserEntity();
        user.setUserId(0);
        //判断用户是否登录
        if (StpUtil.isLogin()) {
            user = (UserEntity) StpUtil.getSession().get(SessionEnum.USER_INFO_KEY);

        }
        Result<SearchResult<ProductDoc>> searchResultResult = searchFeignService.recommendProducts(user.getUserId(), from, size);

        //远程查询用户喜欢的商品推荐信息
        if (Objects.equals(searchResultResult.getCode(), StatusCodeEnum.STATUS200.getCode())) {
            return searchResultResult.getData();
        }

//失败返回错误信息
        throw new RuntimeException(searchResultResult.getMsg());

    }

    /**
     * 商品详情
     *
     * @param id 商品id
     * @return ProductDetailVO 商品详情数据
     */

    @GetMapping("/detail")
    public ProductDetailVO detail(@RequestParam Integer id) {
        ProductDetailVO productDetailVO = productService.getDetail(id);
        return productDetailVO;
    }


    /**
     * 获取商品基本信息
     *
     * @param productId
     * @return
     */
    @RequestMapping("/baseInfo/{productId}")
    public ProductEntity baseInfo(@PathVariable("productId") Integer productId) {
        return productService.getById(productId);
    }


}
